//------------------------------------------------------------------------
// Abstract: Implement of class Console
//
// Authoring: XiaoHanxiong
//
// Time: 2013.12.08
//
// Static test:
//
// Version history:
//		Ver_1.0	: 2013.12.08	XiaoHanxiong
//		Ver_1.1 : 2013.12.30	XiaoHanxiong
//		Ver_1.2 : 2014.03.02	XiaoHanxiong
//------------------------------------------------------------------------
#include "Stdafx.h"
#include "Console.h"
#include "User.h"


namespace Library
{
	// Critical section
	static bool g_critical_setction_flag = false;
	CRITICAL_SECTION g_cs_console;
	CRITICAL_SECTION g_cs_set;


	Console::Console()
	{

	}

	Console::~Console()
	{
		
	}

	void Console::SetColor( StdColor color )
	{
		HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(hStdout, color);
	}

	void Console::SetCriticalSection( bool flag )
	{
		//if(g_critical_setction_flag = flag)
		//	InitializeCriticalSection(&g_cs_console);
		//else
		//	DeleteCriticalSection(&g_cs_console);
	}

	void Console::StdOutA( char* format, ... )
	{
		char buffer[StringBufferSize] = {0};
		va_list args;

		va_start(args, format);
		vsprintf_s(buffer, format, args);
		va_end(args);

		printf("%s: %s\n", User::CurrentTimeA(), buffer);
	}

	void Console::StdOutW( wchar_t* format, ... )
	{
		wchar_t buffer[StringBufferSize] = {0};
		va_list args;

		va_start(args, format);
		vswprintf_s(buffer, format, args);
		va_end(args);

		wprintf(L"%s: %s\n", User::CurrentTimeW(), buffer);
	}

	bool Console::InitializeA( string log, string error )
	{

		return false;
	}

	bool Console::InitializeW( wstring log, wstring error )
	{

		return false;
	}

	void Console::PrintfA( char* format, ... )
	{
		char buffer[StringBufferSize] = {0};
		va_list args;

		va_start(args, format);
		vsprintf_s(buffer, format, args);
		va_end(args);

		SetColor(StdColor_White);
		cout << User::CurrentTimeA() << buffer << '\n';
	}

	void Console::PrintfW( wchar_t* format, ... )
	{
		wchar_t buffer[StringBufferSize] = {0};
		va_list args;

		va_start(args, format);
		vswprintf_s(buffer, format, args);
		va_end(args);

		SetColor(StdColor_White);
		wcout << User::CurrentTimeW() << buffer << (L'\n');
	}

	void Console::ErrorfA( char* format, ... )
	{
		char buffer[StringBufferSize] = {0};
		va_list args;

		va_start(args, format);
		vsprintf_s(buffer, format, args);
		va_end(args);

		SetColor(StdColor_Red);
		cout << User::CurrentTimeA() << buffer << '\n';
	}

	void Console::ErrorfW( wchar_t* format, ... )
	{
		wchar_t buffer[StringBufferSize] = {0};
		va_list args;

		va_start(args, format);
		vswprintf_s(buffer, format, args);
		va_end(args);

		SetColor(StdColor_Red);
		wcout << User::CurrentTimeW() << buffer << (L'\n');
	}

}// _Library_
